﻿using System.Windows;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCommon;
using System;
using Baosight.ZNH.ZMCommon.ChildWindows;

namespace Baosight.ZNH.ZMCS.UI.ChildWindows
{
    /// <summary>
    /// 系统模块的新增、修改、查看页面
    /// 对应后台ServiceZMCS02.java
    /// @author 严洪浩
    /// </summary>
    public partial class Variable : EFViewCmes
    {

        public Variable(string dialogType)
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
            Init(dialogType);
        }

        /// <summary>
        /// 初始化父类EFViewCmesProperty的值，每次点击“确定”、“返回”时执行此方法，从新赋值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.DataFields = this.spWindows.GetChildObjects<EFDataField>(null, true);
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 初始化页面数据
        /// </summary>
        private void Init(string type)
        {
            type = type == null ? "insert" : type.ToLower();

            switch (type)
            {
                case "insert":
                    this.fdIsShow.Value = "0";
                    this.fdDeviationValue.Value = 0;
                    this.fdStatus.Value = "在用";
                    this.fdType.Value = "1";
                    this.fdCycle.Value = "600";
                    base.Mode = OperationMode.Insert;
                    break;
                case "update":
                    base.Mode = OperationMode.Update;
                    break;
                case "detail":
                    this.Commit.Visibility = Visibility.Collapsed;
                    BaseTools.EFDataFieldNotEnabled(this.spWindows.GetChildObjects<EFDataField>(null, true));
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CommitClick(object sender, RoutedEventArgs e)
        {
            base.OnCommitButtonClick("ZMCS45", sender, e);
        }

        /// <summary>
        /// 取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CancelClick(object sender, RoutedEventArgs e)
        {
            CmesWindows _window = this.Parent as CmesWindows;
            _window.DialogResult = false;
            _window.Close();
        }

        /// <summary>
        /// 校验前的工作
        /// </summary>
        /// <returns></returns>
        public override bool ThisValidateField()
        {
            string _value = this.fdInterfaceType.Value == null ? "" : this.fdInterfaceType.Value.ToString();
            if (_value.Equals("0"))
            {
                this.fdTagNo.eRegex = "^[A-Za-z0-9_\\.@]{1,100}$";
                this.fdTagNo.eValidateErrorPrompt = "输入1-100个字符，包括数字、字母、下划线、点号、@符";
                this.fdTableName.eRegex = null;
                this.fdColumn.eRegex = null;
            }
            else
            {
                this.fdTagNo.eRegex = null;
            }
            return true;
        }

        /// <summary>
        /// 根据数据源类型的不同显示不同的输入框
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdInterfaceType_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            if (e.NewValue == null || e.NewValue.ToString().Equals(""))
            {
                return;
            }

            string DataType = e.NewValue.ToString();
            if (DataType.Equals("0"))
            {
                this.fdTagNo.Visibility = Visibility.Visible;
                this.fdTableName.Visibility = Visibility.Collapsed;
                this.fdColumn.Visibility = Visibility.Collapsed;
            }
            else
            {
                this.fdTagNo.Visibility = Visibility.Collapsed;
                this.fdTableName.Visibility = Visibility.Visible;
                this.fdColumn.Visibility = Visibility.Visible;
            }
        }

        /// <summary>
        /// 数据类型，数据初始
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdInterfaceType_Loaded(object sender, RoutedEventArgs e)
        {
            var dataType = this.fdInterfaceType.Value;
            //iHistory数据源
            if (null == dataType || dataType.ToString() == "0" || dataType.ToString() == "")
            {
                this.fdTagNo.Visibility = Visibility.Visible;
                this.fdTableName.Visibility = Visibility.Collapsed;
                this.fdColumn.Visibility = Visibility.Collapsed;
            }
            else
            {
                this.fdTagNo.Visibility = Visibility.Collapsed;
                this.fdTableName.Visibility = Visibility.Visible;
                this.fdColumn.Visibility = Visibility.Visible;
            }
        }

        /// <summary>
        /// 数据源 发生改变时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdDatainterfaceId_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            if (e.NewValue == null)
            {
                return;
            }

            EFDataField field = sender as EFDataField;
            EFComboBox box = field.Content as EFComboBox;
            if (box.SelectedItem == null)
            {
                return;
            }
            string DataType = box.SelectedItem.GetType().GetProperty("type").GetValue(box.SelectedItem, null) as string;
            this.fdInterfaceType.Value = DataType;
        }
    }
}

